package com.dabay.web.job;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.dabay.web.module.News;
import com.dabay.web.module.NewsDetail;
import com.dabay.web.service.News_service;
import com.dabay.web.util.ImagesUtil;
import com.dabay.web.util.JsonPluginsUtil;
import com.dabay.web.util.NbwxUtil;
import com.dabay.web.util.TimeUtil;
/**
 * 只跑一次
 * 1705
 */
@Component
public class MQInitQuartz_News_onlyone {

	private static Logger logger = LoggerFactory.getLogger(MQInitQuartz_News_onlyone.class);

	@Autowired
	private News_service news_service;

	@Scheduled(cron = "20 34 17 ? * *")
	public void runFunction() {
		for(int i=1705;i<1710;i++){
			String methods = "getListByChannelId";
			try {
				System.out.println("=======："+i);
				String result = NbwxUtil.getModelResult_docService(i+"", methods);
				System.out.println(result);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		
	}
	private void sendMQsms_1705() {
		int yesterDayInt = new TimeUtil().getYesterdayInt();
		// 外埠新闻
		String methods = "getListByChannelId";
		String result = "";
		String[] arr_items = new String[] { "1705"};
		List<String>  imagelist = new ArrayList<String>();
		for (String item : arr_items) {
			logger.info("新闻同步开始 新闻类型：-->"+item);
			try {
				result = NbwxUtil.getModelResult_docService(item, methods);
				if (StringUtils.isNotEmpty(result)) {
					try {
						List<com.dabay.web.module.News> newsList = JsonPluginsUtil
								.jsonToBeanList(result,
										com.dabay.web.module.News.class);
						if (newsList.size() > 0) {
							int thisNewReocrdCount = 0;
							for (News news : newsList) {
								try {
									//数据过滤
									int newsDate = new TimeUtil().getIntByDateStr(news.getDocreltime());
									if(newsDate<yesterDayInt){
										continue;
									}
									String docid = news.getDocid();
									int count = news_service.selectNewsById(docid);
									if (count == 0) {
										thisNewReocrdCount++;//记录数加1
										String create_time = TimeUtil.transTime();
										news.setCreate_time(create_time);
										news_service.insertNews(news);
										// 外埠新闻详情
										methods = "getListByDocId";
										result = "";
										try {
											result = NbwxUtil.getModelResult_docService(docid, methods);
											if (StringUtils.isNotEmpty(result)) {
												try {
													List<com.dabay.web.module.NewsDetail>  newsDetailList = JsonPluginsUtil
															.jsonToBeanList(result,com.dabay.web.module.NewsDetail.class);
													if(newsDetailList.size()>0){
														NewsDetail newsDetail = newsDetailList.get(0);
														try {
															count = news_service.selectNewsDetail(docid);			
															if (count == 0) {
																create_time = TimeUtil.transTime();
																newsDetail.setCreate_time(create_time);
																//更新入库图片地址
																String xx = newsDetail.getDochtmlcon();
																String docUrl = newsDetail.getDocpuburl();
																imagelist = ImagesUtil.getImageList_list(docUrl, xx);																
																String image = ImagesUtil.getImageList_String(imagelist);
																newsDetail.setDocimage(image);
																news_service.insertNewsDetail(newsDetail);
															}
														} catch (Exception e) {
															e.printStackTrace();
															logger.info("error!! the data insert shibai == the docid is"+docid);
															logger.info(new Date()
																	+ " 新闻插入失败 --> "+item);
														}

													}													
												} catch (Exception e) {
													e.printStackTrace();
												}
											}
										} catch (Exception e) {
											e.printStackTrace();
										}

									}
								} catch (Exception e) {
									e.printStackTrace();
									logger.info(new Date()
											+ " 插入数据库失败 --> 新闻数据类型"+item);
								}
							}
							logger.info(item+"新闻同步结束 共同步条数：-->"+thisNewReocrdCount);
						}
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
	private void sendMQsms_1706() {
		int yesterDayInt = new TimeUtil().getYesterdayInt();
		// 外埠新闻
		String methods = "getListByChannelId";
		String result = "";
		String[] arr_items = new String[] { "1706"};
		List<String>  imagelist = new ArrayList<String>();
		for (String item : arr_items) {
			logger.info("新闻同步开始 新闻类型：-->"+item);
			try {
				result = NbwxUtil.getModelResult_docService(item, methods);
				if (StringUtils.isNotEmpty(result)) {
					try {
						List<com.dabay.web.module.News> newsList = JsonPluginsUtil
								.jsonToBeanList(result,
										com.dabay.web.module.News.class);
						if (newsList.size() > 0) {
							int thisNewReocrdCount = 0;
							for (News news : newsList) {
								try {
									//数据过滤
									int newsDate = new TimeUtil().getIntByDateStr(news.getDocreltime());
									if(newsDate<yesterDayInt){
										continue;
									}
									String docid = news.getDocid();
									int count = news_service.selectNewsById(docid);
									if (count == 0) {
										thisNewReocrdCount++;//记录数加1
										String create_time = TimeUtil.transTime();
										news.setCreate_time(create_time);
										news_service.insertNews(news);
										// 外埠新闻详情
										methods = "getListByDocId";
										result = "";
										try {
											result = NbwxUtil.getModelResult_docService(docid, methods);
											if (StringUtils.isNotEmpty(result)) {
												try {
													List<com.dabay.web.module.NewsDetail>  newsDetailList = JsonPluginsUtil
															.jsonToBeanList(result,com.dabay.web.module.NewsDetail.class);
													if(newsDetailList.size()>0){
														NewsDetail newsDetail = newsDetailList.get(0);
														try {
															count = news_service.selectNewsDetail(docid);			
															if (count == 0) {
																create_time = TimeUtil.transTime();
																newsDetail.setCreate_time(create_time);
																//更新入库图片地址
																String xx = newsDetail.getDochtmlcon();
																String docUrl = newsDetail.getDocpuburl();
																imagelist = ImagesUtil.getImageList_list(docUrl, xx);																
																String image = ImagesUtil.getImageList_String(imagelist);
																newsDetail.setDocimage(image);
																news_service.insertNewsDetail(newsDetail);
															}
														} catch (Exception e) {
															e.printStackTrace();
															logger.info("error!! the data insert shibai == the docid is"+docid);
															logger.info(new Date()
																	+ " 新闻插入失败 --> "+item);
														}

													}													
												} catch (Exception e) {
													e.printStackTrace();
												}
											}
										} catch (Exception e) {
											e.printStackTrace();
										}

									}
								} catch (Exception e) {
									e.printStackTrace();
									logger.info(new Date()
											+ " 插入数据库失败 --> 新闻数据类型"+item);
								}
							}
							logger.info(item+"新闻同步结束 共同步条数：-->"+thisNewReocrdCount);
						}
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
	private void sendMQsms_1707() {
		int yesterDayInt = new TimeUtil().getYesterdayInt();
		// 外埠新闻
		String methods = "getListByChannelId";
		String result = "";
		String[] arr_items = new String[] { "1707"};
		List<String>  imagelist = new ArrayList<String>();
		for (String item : arr_items) {
			logger.info("新闻同步开始 新闻类型：-->"+item);
			try {
				result = NbwxUtil.getModelResult_docService(item, methods);
				if (StringUtils.isNotEmpty(result)) {
					try {
						List<com.dabay.web.module.News> newsList = JsonPluginsUtil
								.jsonToBeanList(result,
										com.dabay.web.module.News.class);
						if (newsList.size() > 0) {
							int thisNewReocrdCount = 0;
							for (News news : newsList) {
								try {
									//数据过滤
									int newsDate = new TimeUtil().getIntByDateStr(news.getDocreltime());
									if(newsDate<yesterDayInt){
										continue;
									}
									String docid = news.getDocid();
									int count = news_service.selectNewsById(docid);
									if (count == 0) {
										thisNewReocrdCount++;//记录数加1
										String create_time = TimeUtil.transTime();
										news.setCreate_time(create_time);
										news_service.insertNews(news);
										// 外埠新闻详情
										methods = "getListByDocId";
										result = "";
										try {
											result = NbwxUtil.getModelResult_docService(docid, methods);
											if (StringUtils.isNotEmpty(result)) {
												try {
													List<com.dabay.web.module.NewsDetail>  newsDetailList = JsonPluginsUtil
															.jsonToBeanList(result,com.dabay.web.module.NewsDetail.class);
													if(newsDetailList.size()>0){
														NewsDetail newsDetail = newsDetailList.get(0);
														try {
															count = news_service.selectNewsDetail(docid);			
															if (count == 0) {
																create_time = TimeUtil.transTime();
																newsDetail.setCreate_time(create_time);
																//更新入库图片地址
																String xx = newsDetail.getDochtmlcon();
																String docUrl = newsDetail.getDocpuburl();
																imagelist = ImagesUtil.getImageList_list(docUrl, xx);																
																String image = ImagesUtil.getImageList_String(imagelist);
																newsDetail.setDocimage(image);
																news_service.insertNewsDetail(newsDetail);
															}
														} catch (Exception e) {
															e.printStackTrace();
															logger.info("error!! the data insert shibai == the docid is"+docid);
															logger.info(new Date()
																	+ " 新闻插入失败 --> "+item);
														}

													}													
												} catch (Exception e) {
													e.printStackTrace();
												}
											}
										} catch (Exception e) {
											e.printStackTrace();
										}

									}
								} catch (Exception e) {
									e.printStackTrace();
									logger.info(new Date()
											+ " 插入数据库失败 --> 新闻数据类型"+item);
								}
							}
							logger.info(item+"新闻同步结束 共同步条数：-->"+thisNewReocrdCount);
						}
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
	private void sendMQsms_1708() {
		int yesterDayInt = new TimeUtil().getYesterdayInt();
		// 外埠新闻
		String methods = "getListByChannelId";
		String result = "";
		String[] arr_items = new String[] { "1708"};
		List<String>  imagelist = new ArrayList<String>();
		for (String item : arr_items) {
			logger.info("新闻同步开始 新闻类型：-->"+item);
			try {
				result = NbwxUtil.getModelResult_docService(item, methods);
				if (StringUtils.isNotEmpty(result)) {
					try {
						List<com.dabay.web.module.News> newsList = JsonPluginsUtil
								.jsonToBeanList(result,
										com.dabay.web.module.News.class);
						if (newsList.size() > 0) {
							int thisNewReocrdCount = 0;
							for (News news : newsList) {
								try {
									//数据过滤
									int newsDate = new TimeUtil().getIntByDateStr(news.getDocreltime());
									if(newsDate<yesterDayInt){
										continue;
									}
									String docid = news.getDocid();
									int count = news_service.selectNewsById(docid);
									if (count == 0) {
										thisNewReocrdCount++;//记录数加1
										String create_time = TimeUtil.transTime();
										news.setCreate_time(create_time);
										news_service.insertNews(news);
										// 外埠新闻详情
										methods = "getListByDocId";
										result = "";
										try {
											result = NbwxUtil.getModelResult_docService(docid, methods);
											if (StringUtils.isNotEmpty(result)) {
												try {
													List<com.dabay.web.module.NewsDetail>  newsDetailList = JsonPluginsUtil
															.jsonToBeanList(result,com.dabay.web.module.NewsDetail.class);
													if(newsDetailList.size()>0){
														NewsDetail newsDetail = newsDetailList.get(0);
														try {
															count = news_service.selectNewsDetail(docid);			
															if (count == 0) {
																create_time = TimeUtil.transTime();
																newsDetail.setCreate_time(create_time);
																//更新入库图片地址
																String xx = newsDetail.getDochtmlcon();
																String docUrl = newsDetail.getDocpuburl();
																imagelist = ImagesUtil.getImageList_list(docUrl, xx);																
																String image = ImagesUtil.getImageList_String(imagelist);
																newsDetail.setDocimage(image);
																news_service.insertNewsDetail(newsDetail);
															}
														} catch (Exception e) {
															e.printStackTrace();
															logger.info("error!! the data insert shibai == the docid is"+docid);
															logger.info(new Date()
																	+ " 新闻插入失败 --> "+item);
														}

													}													
												} catch (Exception e) {
													e.printStackTrace();
												}
											}
										} catch (Exception e) {
											e.printStackTrace();
										}

									}
								} catch (Exception e) {
									e.printStackTrace();
									logger.info(new Date()
											+ " 插入数据库失败 --> 新闻数据类型"+item);
								}
							}
							logger.info(item+"新闻同步结束 共同步条数：-->"+thisNewReocrdCount);
						}
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
	private void sendMQsms_1709() {
		int yesterDayInt = new TimeUtil().getYesterdayInt();
		// 外埠新闻
		String methods = "getListByChannelId";
		String result = "";
		String[] arr_items = new String[] { "1709"};
		List<String>  imagelist = new ArrayList<String>();
		for (String item : arr_items) {
			logger.info("新闻同步开始 新闻类型：-->"+item);
			try {
				result = NbwxUtil.getModelResult_docService(item, methods);
				if (StringUtils.isNotEmpty(result)) {
					try {
						List<com.dabay.web.module.News> newsList = JsonPluginsUtil
								.jsonToBeanList(result,
										com.dabay.web.module.News.class);
						if (newsList.size() > 0) {
							int thisNewReocrdCount = 0;
							for (News news : newsList) {
								try {
									//数据过滤
									int newsDate = new TimeUtil().getIntByDateStr(news.getDocreltime());
									if(newsDate<yesterDayInt){
										continue;
									}
									String docid = news.getDocid();
									int count = news_service.selectNewsById(docid);
									if (count == 0) {
										thisNewReocrdCount++;//记录数加1
										String create_time = TimeUtil.transTime();
										news.setCreate_time(create_time);
										news_service.insertNews(news);
										// 外埠新闻详情
										methods = "getListByDocId";
										result = "";
										try {
											result = NbwxUtil.getModelResult_docService(docid, methods);
											if (StringUtils.isNotEmpty(result)) {
												try {
													List<com.dabay.web.module.NewsDetail>  newsDetailList = JsonPluginsUtil
															.jsonToBeanList(result,com.dabay.web.module.NewsDetail.class);
													if(newsDetailList.size()>0){
														NewsDetail newsDetail = newsDetailList.get(0);
														try {
															count = news_service.selectNewsDetail(docid);			
															if (count == 0) {
																create_time = TimeUtil.transTime();
																newsDetail.setCreate_time(create_time);
																//更新入库图片地址
																String xx = newsDetail.getDochtmlcon();
																String docUrl = newsDetail.getDocpuburl();
																imagelist = ImagesUtil.getImageList_list(docUrl, xx);																
																String image = ImagesUtil.getImageList_String(imagelist);
																newsDetail.setDocimage(image);
																news_service.insertNewsDetail(newsDetail);
															}
														} catch (Exception e) {
															e.printStackTrace();
															logger.info("error!! the data insert shibai == the docid is"+docid);
															logger.info(new Date()
																	+ " 新闻插入失败 --> "+item);
														}

													}													
												} catch (Exception e) {
													e.printStackTrace();
												}
											}
										} catch (Exception e) {
											e.printStackTrace();
										}

									}
								} catch (Exception e) {
									e.printStackTrace();
									logger.info(new Date()
											+ " 插入数据库失败 --> 新闻数据类型"+item);
								}
							}
							logger.info(item+"新闻同步结束 共同步条数：-->"+thisNewReocrdCount);
						}
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}